﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace TunerControl
{
    public partial class Tuner : UserControl
    {
        private int x1 = 30;
        private int x2 = 135;
        private int x3 = 235;
        private int x4 = 335;
        private int y1 = 140;
        private int y2 = 60;
        private int y3 = 60;
        private int y4 = 140;
        private float angle;
        private float WIDTH;
        private float HEIGHT;
        private double freq;

        public Tuner()
        {
            InitializeComponent();
            WIDTH = this.Width;
            HEIGHT = this.Height;
        }

        public string LowNote
        {
            get { return LowLabel.Text; }
            set { LowLabel.Text = value; Invalidate(); }
        }

        public string Note
        {
            get { return NoteLabel.Text; }
            set { NoteLabel.Text = value; Invalidate(); }
        }

        public string HighNote
        {
            get { return HighLabel.Text; }
            set { HighLabel.Text = value; Invalidate(); }
        }

        public float Angle
        {
            get { return angle; }
            set { angle = value;
            if (angle == 90)
            {
                NoteLabel.BackColor = Color.Lime;
            }
            else
            {
                NoteLabel.BackColor = System.Drawing.SystemColors.Control;
            }
                Invalidate(); }
        }

        public double Frequency
        {
            get { return freq; }
            set { freq = value; FreqLabel.Text = freq.ToString("0,0.000") + " Hz"; Invalidate(); }
        }

        private void Tuner_Paint(object sender, PaintEventArgs e)
        {
            Pen black = new Pen(Brushes.Black, 2);
            float scaleX = this.Width / WIDTH;
            float scaleY = this.Height / HEIGHT;
            e.Graphics.DrawBezier(black,
                new PointF(x1 * scaleX, y1 * scaleY),
                new PointF(x2 * scaleX, y2 * scaleY),
                new PointF(x3 * scaleX, y3 * scaleY),
                new PointF(x4 * scaleX, y4 * scaleY)
                );
            double radians = angle * Math.PI / 180;
            Point origin = new Point(this.Width / 2, this.Height - 20);
            //int length = 175;
            //e.Graphics.DrawLine(black,
            //    origin,
            //    new Point((int)(origin.X - Math.Cos(radians) * length * scaleX),
            //        (int)(origin.Y - Math.Sin(radians) * length * scaleY)
            //        ));

            int x = this.Width / 2;
            Image pic = Properties.Resources.arrow;
            Graphics g = e.Graphics;
            Size scale = pic.Size;
            scale.Width = (int)(185 * scaleX);
            scale.Height = (int)(20 * scaleY);
            // move starting origin to middle bottom of screen
            g.TranslateTransform(x, this.Height - 20 * scaleY);

            // rotate by angle
            g.RotateTransform(180 + angle);

            // move y-origin back to start
            g.TranslateTransform(-5, -scale.Height / 2);
            g.DrawImage(pic, new Rectangle(new Point(0, 0), scale));

        }

        private void Tuner_SizeChanged(object sender, EventArgs e)
        {
            float scaleX = this.Width / WIDTH;
            float scaleY = this.Height / HEIGHT;
            LowLabel.Location = new Point((int)(this.Width / 6 - 45 * scaleX),
                (int)(this.Width / 5 - 10 * scaleY));
            NoteLabel.Location = new Point((int)(this.Width / 2 - NoteLabel.Size.Width / 2),
                (int)(this.Width / 5 - 40 * scaleY));
            HighLabel.Location = new Point((int)((5 * this.Width / 6) - 10 * scaleX),
                (int)(this.Width / 5 - 10 * scaleY));
            FreqLabel.Location = new Point((int)(this.Width / 2 - FreqLabel.Size.Width / 2),
                (int)(this.Width / 5 - 10 * scaleY));
        }

        private void FreqLabel_TextChanged(object sender, EventArgs e)
        {
            Point pt = FreqLabel.Location;
            pt.X = (int)(this.Width / 2 - FreqLabel.Size.Width / 2);
            FreqLabel.Location = pt;
        }
    }
}
